#include "game/oogameworld.h"
#include "graphics/oorender.h"
#include "graphics/ootexture.h"
#include "scene/oogamelevel.h"
#include "resource/ooresourcemanager.h"

//------------------------------------------------------------------------
ImplementSingleton(ooGameWorld)

//------------------------------------------------------------------------
ooGameWorld::ooGameWorld()
:m_gameLevel(DBBNULL)
{
	_init();
	ConstructSingleton
}

//------------------------------------------------------------------------
ooGameWorld::~ooGameWorld()
{
	_cleanup();
	DestructSingleton
}

//------------------------------------------------------------------------
void 
ooGameWorld::tick( float elapsed )
{
	m_resourceManager->tick(elapsed);

	ooRender::GetSingleton()->begin2D();
	glColor3f(1,1,1);
	glRectf(0,0,512,512);
	struct MyQuad
	{
		float tex[2];
		uint32 color;
		float vertex[3];
	};
	MyQuad myquad[6];

	glInterleavedArrays(GL_T2F_C4UB_V3F , 0, myquad);
	Ptr<ooTexture> tex	= m_resourceManager->loadTextureResource("E:\\resource\\ooEngine\\texture\\oot1.xml");
	ooRender::GetSingleton()->bindTexture(tex);
	//vert0
	myquad[0].vertex[0] = 0;
	myquad[0].vertex[1] = 512;
	myquad[0].vertex[2] = 0;
	myquad[0].color     = 0XFFFFFFFF;
	myquad[0].tex[0]    = 0;
	myquad[0].tex[1]    = 1;

	//vert1
	myquad[1].vertex[0] = 0;
	myquad[1].vertex[1] = 0;
	myquad[1].vertex[2] = 0;
	myquad[1].color     = 0XFFFFFFFF;
	myquad[1].tex[0]    = 0;
	myquad[1].tex[1]    = 0;

	//vert2
	myquad[2].vertex[0] = 512;
	myquad[2].vertex[1] = 512;
	myquad[2].vertex[2] = 0;
	myquad[2].color     = 0XFFFFFFFF;
	myquad[2].tex[0]    = 1;
	myquad[2].tex[1]    = 1;

	////vert3
	//myquad[3].vertex[0] = tex->getWidth();
	//myquad[3].vertex[1] = 0;
	//myquad[3].vertex[2] = 0;
	//myquad[3].color     = 0XFFFFFFFF;
	//myquad[3].tex[0]    = 1;
	//myquad[3].tex[1]    = 0;

	////vert4
	//myquad[4].vertex[0] = 0;
	//myquad[4].vertex[1] = tex->getHeight();
	//myquad[4].vertex[2] = 0;
	//myquad[4].color     = 0XFFFFFFFF;
	//myquad[4].tex[0]    = 0;
	//myquad[4].tex[1]    = 1;

	////vert5
	//myquad[5].vertex[0] = tex->getWidth();
	//myquad[5].vertex[1] = tex->getHeight();
	//myquad[5].vertex[2] = 0;
	//myquad[5].color     = 0XFFFFFFFF;
	//myquad[5].tex[0]    = 1;
	//myquad[5].tex[1]    = 1;

	glDrawArrays(GL_TRIANGLES, 0, 3);
	ooRender::GetSingleton()->end2D();
	ooRender::GetSingleton()->present();
}

//------------------------------------------------------------------------
void 
ooGameWorld::loadGameLevel( const String& filename )
{
	if(DBBNULL!=m_gameLevel)
	{
		m_gameLevel->onLevelEnding();
		delete m_gameLevel;
	}
	m_gameLevel = new ooGameLevel(filename);
	m_gameLevel->onLevelLoading();
}

//------------------------------------------------------------------------
void
ooGameWorld::_init()
{
	m_resourceManager	= new ooResourceManager();
}

//------------------------------------------------------------------------
void
ooGameWorld::_cleanup()
{
	delete m_resourceManager;
}